﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using TGB.Core.Infrastructure;
using TGBTech.Core.Models.DanhMuc;
using PagedList;

namespace TGBTech.WebAdmin.Controllers
{
    public class ChiTietHangHoaController : Controller
    {
        private TGBTechDb db = new TGBTechDb();
        private const int pageSize = 100;

        // GET: ChiTietHangHoa
        public ActionResult Index(int? page, string searchString)
        {
            var CTHHs = db.ChiTietHangHoa.Where(p => p.State == true);

            // Search by string            
            if (!String.IsNullOrEmpty(searchString))
            {
                CTHHs = CTHHs.Where(p => p.HangHoa.MaHangHoa.ToUpper().Contains(searchString.ToUpper())
                    || p.HangHoa.Name.ToUpper().Contains(searchString.ToUpper())
                    || p.OtherName.ToUpper().Contains(searchString.ToUpper())
                    );
            }

            // Orderby list
            CTHHs = CTHHs.OrderBy(p => p.State);

            // Calculate page index
            int index = CTHHs.Count() / pageSize + 1;
            int pageNumber = (page ?? index);

            return View(CTHHs.ToPagedList(pageNumber, pageSize));
        }

        // GET: ChiTietHangHoa/Create
        public ActionResult Create()
        {
            ViewBag.List = db.ChiTietHangHoa.Where(p => p.State == true).OrderByDescending(p => p.Id).Take(5).OrderBy(p => p.Id).ToList();

            var listHangHoa = db.HangHoa.Where(p => p.State == true)
                .Select(p => new
                {
                    Id = p.Id,
                    Display = p.MaHangHoa + " | " + p.Name
                }).ToList();

            ViewBag.HangHoaId = new SelectList(listHangHoa, "Id", "Display");
            return View();
        }

        // POST: ChiTietHangHoa/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,HangHoaId,OtherName,SoLo,QuyCach,DVT,VAT,NgayNhap,NgayHetHan,DonGiaNhap,SoLuongTon,State")] ChiTietHangHoa chiTietHangHoa)
        {
            if (ModelState.IsValid)
            {
                chiTietHangHoa.State = true;
                db.ChiTietHangHoa.Add(chiTietHangHoa);
                db.SaveChanges();
                return RedirectToAction("Create");
            }

            return View(chiTietHangHoa);
        }

        // GET: ChiTietHangHoa/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ChiTietHangHoa chiTietHangHoa = db.ChiTietHangHoa.Find(id);
            if (chiTietHangHoa == null)
            {
                return HttpNotFound();
            }

            var listHangHoa = db.HangHoa.Where(p => p.State == true)
                .Select(p => new
                {
                    Id = p.Id,
                    Display = p.MaHangHoa + " | " + p.Name
                }).ToList();

            ViewBag.HangHoaId = new SelectList(listHangHoa, "Id", "Display");

            return View(chiTietHangHoa);
        }

        // POST: ChiTietHangHoa/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,HangHoaId,OtherName,SoLo,QuyCach,DVT,VAT,NgayNhap,NgayHetHan,DonGiaNhap,SoLuongTon,State")] ChiTietHangHoa chiTietHangHoa)
        {
            if (ModelState.IsValid)
            {
                db.Entry(chiTietHangHoa).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(chiTietHangHoa);
        }

        // GET: ChiTietHangHoa/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ChiTietHangHoa chiTietHangHoa = db.ChiTietHangHoa.Find(id);
            if (chiTietHangHoa == null)
            {
                return HttpNotFound();
            }
            return View(chiTietHangHoa);
        }

        // POST: ChiTietHangHoa/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ChiTietHangHoa chiTietHangHoa = db.ChiTietHangHoa.Find(id);
            chiTietHangHoa.State = false;
            db.Entry(chiTietHangHoa).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
